﻿// DataProviders/OracleProvider.cs

using System.Collections.Generic;
using System.Linq;
using SqlSugar;
using DatabaseManager.Core.Models;

namespace DatabaseManager.DataProviders
{
    public class OracleProvider : IDataProvider
    {
        private readonly ISqlSugarClient _db;

        public OracleProvider(ISqlSugarClient db)
        {
            _db = db;
        }

        public bool TestConnection() => _db.Ado.GetScalar("SELECT 1 FROM DUAL") != null;

        public List<TableInfo> GetTables()
        {
            var sql = @"SELECT TABLE_NAME AS Name, OWNER AS Schema, COMMENTS AS Description FROM ALL_TABLE_COMMENTS WHERE OWNER NOT IN ('SYS', 'SYSTEM')";
            return _db.Ado.SqlQuery<TableInfo>(sql);
        }

        public List<ColumnInfo> GetColumns(string tableName)
        {
            var sql = $@"SELECT 
                            COLUMN_NAME AS Name,
                            DATA_TYPE AS DataType,
                            NULLABLE AS IsNullable,
                            DATA_DEFAULT AS DefaultValue,
                            COMMENTS AS Description,
                            COLUMN_ID AS OrdinalPosition,
                            DECODE(CONSTRAINT_TYPE, 'P', 1, 0) AS IsPrimaryKey
                         FROM ALL_CONS_COLUMNS ACC
                         JOIN ALL_CONSTRAINTS AC ON ACC.CONSTRAINT_NAME = AC.CONSTRAINT_NAME
                         RIGHT JOIN ALL_CONS_COLUMNS C ON C.TABLE_NAME = '{tableName}'";

            return _db.Ado.SqlQuery<ColumnInfo>(sql);
        }

        public List<string> GetDatabases()
        {
            return new List<string>(); // Oracle 不支持“数据库”概念
        }
    }
}